package com.tanhua.dubbo.api;


import com.tanhua.api.CommentApi;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public Integer save(Comment newComment) {
        //保存评论互动
        // 1，判断动态是否存在
        Movement movement = mongoTemplate.findById(newComment.getPublishId(), Movement.class);
        if (movement == null){
            throw  new RuntimeException("动态不存在");
        }
        newComment.setPublishUserId(movement.getUserId());
        mongoTemplate.save(newComment);

        //更新动态表里面的评论数，互动数
        Query query = Query.query(Criteria.where("id").is(newComment.getPublishId()));
        Update update = new Update();
        if (newComment.getCommentType()== CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else if(newComment.getCommentType()==CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        }else if(newComment.getCommentType()==CommentType.LOVE.getType()) {
            update.inc("loveCount", 1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Movement andModify = mongoTemplate.findAndModify(query, update, options, Movement.class);
   return andModify.statisCount(newComment.getCommentType());
    }

    @Override
    public List<Comment> findComments(String movementId, int page, int pageSize, int type) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(type));
        query.skip((page - 1)*pageSize)
                .limit(pageSize).
                with(Sort.by(Sort.Order.desc("created")));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        return commentList;
    }

    @Override
    public Boolean haslikeComment(String movementId, Long getid,int commentType) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("userId").is(getid).and("commentType").is(commentType));
        return    mongoTemplate.exists(query,Comment.class);

    }

    /**
     * 删除互动表里面的点赞数据，并修改动态里面的数据
     * @param movementId
     * @param getid
     * @param type
     * @return
     */
    @Override
    public Integer delete(String movementId, Long getid, int type) {
        //删除圈子互动的数据
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId)).and("userId").is(getid).and("commentType").is(type));
        mongoTemplate.remove(query, Comment.class);
       //修改动态里面的数据
        Query query1 = new Query(Criteria.where("id").is(new ObjectId(movementId)));
        Update update = new Update();
        if (type== CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else if(type==CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        }else if(type==CommentType.LOVE.getType()) {
            update.inc("loveCount", -1);
        }

        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement movement = mongoTemplate.findAndModify(query1, update, options, Movement.class);
        return movement.statisCount(type);
    }
}
